﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	public class CreatPipeTagsNew : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			if (commandData == null)
			{
				throw new System.ArgumentNullException();
			}
			UIApplication application = commandData.Application;
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			Autodesk.Revit.DB.View activeView = activeUIDocument.ActiveView;
			Selection selection = activeUIDocument.Selection;
			if (!YJKRevitTools.ViewCheck(activeUIDocument, new List<ViewType>
			{
				(ViewType)1,
				(ViewType)2,
				(ViewType)4,
				(ViewType)117
			}))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			HYTagOperate hytagOperate = new HYTagOperate(commandData);
			Result result;
			try
			{
				activeView = activeUIDocument.ActiveView;
				if ((int)activeView.ViewType == 4 || (int)activeView.ViewType == 117)
				{
					if ((int)activeView.ViewType == 4 && !(activeView as View3D).IsLocked)
					{
						YJKMessageBox.Warning("该三维视图无法标注，请锁定视角后再进行此操作！");
						result = Autodesk.Revit.UI.Result.Cancelled;
					}
					else
					{
						PipeMarkForm pipeMarkForm = new PipeMarkForm(true);
						DialogResult dialogResult = DialogResult.OK;
						do
						{
							if (dialogResult == DialogResult.OK)
							{
								dialogResult = pipeMarkForm.ShowDialog();
							}
							if (dialogResult == DialogResult.Yes)
							{
								transaction = new Transaction(document, "水管标注");
								transaction.Start();
								string tagName = this.GetTagName(pipeMarkForm.markContent);
								hytagOperate.HYLoadFamily(HYTagOperate.MarkTagType.pipe.ToString(), "HY_水管标注");
								string markStyle = pipeMarkForm.markStyle;
								if (!(markStyle == "single"))
								{
									if (markStyle == "auto")
									{
										hytagOperate.MarkAutoPipe(tagName, pipeMarkForm.pipeTagDis, pipeMarkForm.markUpMiddleDown, pipeMarkForm.IsDelete, pipeMarkForm.isMarkbyBJ, pipeMarkForm.minPipeLength, pipeMarkForm.MinDia);
									}
								}
								else
								{
									hytagOperate.MarkSinglePipe3D(tagName, pipeMarkForm.pipeTagDis, pipeMarkForm.markUpMiddleDown, pipeMarkForm.IsDelete);
								}
								dialogResult = DialogResult.Yes;
								document.Regenerate();
								transaction.Commit();
							}
						}
						while (dialogResult != DialogResult.Cancel);
						pipeMarkForm.Close();
						result = Autodesk.Revit.UI.Result.Succeeded;
					}
				}
				else
				{
					PipeMarkForm pipeMarkForm2 = new PipeMarkForm(false);
					DialogResult dialogResult2 = DialogResult.OK;
					do
					{
						if (dialogResult2 == DialogResult.OK)
						{
							dialogResult2 = pipeMarkForm2.ShowDialog();
						}
						if (dialogResult2 == DialogResult.Yes)
						{
							transaction = new Transaction(document, "水管标注");
							transaction.Start();
							string tagName2 = this.GetTagName(pipeMarkForm2.markContent);
							hytagOperate.HYLoadFamily(HYTagOperate.MarkTagType.pipe.ToString(), "HY_水管标注");
							string markStyle = pipeMarkForm2.markStyle;
							if (!(markStyle == "single"))
							{
								if (!(markStyle == "auto"))
								{
									if (markStyle == "multiply")
									{
										hytagOperate.MarkMultiplyPipe(tagName2, pipeMarkForm2.pipeTagDis, pipeMarkForm2.isHaveSlash, pipeMarkForm2.markUpMiddleDown, pipeMarkForm2.IsDelete, pipeMarkForm2.isTagDis);
									}
								}
								else
								{
									hytagOperate.MarkAutoPipe(tagName2, pipeMarkForm2.pipeTagDis, pipeMarkForm2.markUpMiddleDown, pipeMarkForm2.IsDelete, pipeMarkForm2.isMarkbyBJ, pipeMarkForm2.minPipeLength, pipeMarkForm2.MinDia);
								}
							}
							else
							{
								hytagOperate.MarkSinglePipe(tagName2, pipeMarkForm2.pipeTagDis, pipeMarkForm2.markUpMiddleDown, pipeMarkForm2.IsDelete);
							}
							dialogResult2 = DialogResult.Yes;
							document.Regenerate();
							transaction.Commit();
						}
					}
					while (dialogResult2 != DialogResult.Cancel);
					pipeMarkForm2.Close();
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null)
				{
					TransactionStatus status = transaction.GetStatus();
					if ((int)status == 1)
					{
						transaction.Commit();
					}
					else if ((int)status == 5)
					{
						transaction.RollBack();
					}
				}
			}
			return result;
		}

		private string GetTagName(string styleName)
		{
			string result = "";
			uint num = YArchitech.Revit.PrivateImplementationDetails.ComputeStringHash(styleName);
			if (num <= 3944395331u)
			{
				if (num <= 2065846456u)
				{
					if (num != 1948256028u)
					{
						if (num != 1998588885u)
						{
							if (num == 2065846456u)
							{
								if (styleName == "style51")
								{
									result = "水管外径";
								}
							}
						}
						else if (styleName == "style41")
						{
							result = "系统标高外径";
						}
					}
					else if (styleName == "style42")
					{
						result = "系统标高管径外径";
					}
				}
				else if (num <= 2200214503u)
				{
					if (num != 2116179313u)
					{
						if (num == 2200214503u)
						{
							if (styleName == "style21")
							{
								result = "系统外径";
							}
						}
					}
					else if (styleName == "style52")
					{
						result = "水管管径外径";
					}
				}
				else if (num != 2216992122u)
				{
					if (num == 3944395331u)
					{
						if (styleName == "style61")
						{
							result = "标高外径";
						}
					}
				}
				else if (styleName == "style22")
				{
					result = "系统管径外径";
				}
			}
			else if (num <= 4065836486u)
			{
				if (num <= 4032281248u)
				{
					if (num != 3961172950u)
					{
						if (num == 4032281248u)
						{
							if (styleName == "style6")
							{
								result = "标高管径";
							}
						}
					}
					else if (styleName == "style62")
					{
						result = "标高管径外径";
					}
				}
				else if (num != 4049058867u)
				{
					if (num == 4065836486u)
					{
						if (styleName == "style4")
						{
							result = "系统标高管径";
						}
					}
				}
				else if (styleName == "style7")
				{
					result = "水管标高";
				}
			}
			else if (num <= 4099391724u)
			{
				if (num != 4082614105u)
				{
					if (num == 4099391724u)
					{
						if (styleName == "style2")
						{
							result = "系统管径";
						}
					}
				}
				else if (styleName == "style5")
				{
					result = "水管管径";
				}
			}
			else if (num != 4116169343u)
			{
				if (num == 4149724581u)
				{
					if (styleName == "style1")
					{
						result = "水管类型";
					}
				}
			}
			else if (styleName == "style3")
			{
				result = "系统标高";
			}
			return result;
		}
	}
}
